package org.szgb.console.sys.custom.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysCustomForm extends BeanEntity<SysCustomForm> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;
    private Boolean isDynamic;

    public SysCustomForm() {
    }

    public SysCustomForm(String id) {
        this.setId(id);
    }

    public SysCustomForm(SysCustomForm.SysCustomFormFindDto SysCustomFormFindDto){
        this.setId(SysCustomFormFindDto.getId());
        this.setName(SysCustomFormFindDto.getName());
        this.setIsDynamic(SysCustomFormFindDto.getIsDynamic());
        this.setPageDto(SysCustomFormFindDto.getPageDto());
    }

    public SysCustomForm(SysCustomForm.SysCustomFormSaveDto SysCustomFormSaveDto){
        this.setId(SysCustomFormSaveDto.id);
        this.setName(SysCustomFormSaveDto.name);
        this.setIsDynamic(SysCustomFormSaveDto.isDynamic);
    }

    public SysCustomForm.SysCustomFormView toSysCustomFormView() {
        return new SysCustomForm.SysCustomFormView(this);
    }

    @Data
    @Schema(title = "自定义表单信息", description = "自定义表单信息")
    public static class SysCustomFormView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "是否动态表单")
        private Boolean isDynamic;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysCustomFormView(SysCustomForm SysCustomForm) {
            this.id = SysCustomForm.getId();
            this.name = SysCustomForm.getName();
            this.isDynamic = SysCustomForm.getIsDynamic();
            this.createdAt = SysCustomForm.getCreatedAt();
            this.updatedAt = SysCustomForm.getUpdatedAt();
            if (SysCustomForm.getCreatedBy() != null) {
                this.createdById = SysCustomForm.getCreatedBy().getId();
                this.createdByName = SysCustomForm.getCreatedBy().getName();
            }
            if (SysCustomForm.getUpdatedBy() != null) {
                this.updatedById = SysCustomForm.getUpdatedBy().getId();
                this.updatedByName = SysCustomForm.getUpdatedBy().getName();
            }
        }

        public static List<SysCustomForm.SysCustomFormView> getViewListFromList(List<SysCustomForm> SysCustomFormList) {
            return SysCustomFormList.stream().map(SysCustomForm.SysCustomFormView::new).toList();
        }
    }

    @Data
    @Schema(title = "自定义表单查询表单", description = "自定义表单查询参数")
    public static class SysCustomFormFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "是否动态表单")
        private Boolean isDynamic;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysCustomForm toSysCustomForm() {
            return new SysCustomForm(this);
        }
    }

    @Data
    @Schema(title = "自定义表单保存表单", description = "自定义表单保存参数")
    public static class SysCustomFormSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "是否动态表单")
        private Boolean isDynamic;

        public SysCustomForm toSysCustomForm() {
            return new SysCustomForm(this);
        }
    }
}
